<template>
	<div class="system-role-container">
		<el-card shadow="hover" class="mb10" v-auth="'system:role:list'">
			<el-form :inline="true" :model="search">
				<el-form-item>
					<el-input v-model="search.roleName" placeholder="请输入角色名称" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="search.roleKey" placeholder="请输入角色权限字符" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item>
					<el-date-picker type="daterange" align="right" v-model="dateData.value" :shortcuts="dateData.shortcuts"
						unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
					<el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="hover">
			<el-button v-auth="'system:role:add'" type="primary" class="ml10" icon="el-icon-document-add" @click="onOpenAddRole()">新增</el-button>
			<el-button v-auth="'system:role:edit'" type="primary" class="ml10" icon="el-icon-edit" @click="onOpenEditRole()" :disabled="tableData.selections.length != 1">修改</el-button>
			<el-button v-auth="'system:role:remove'" type="primary" class="ml10" icon="el-icon-delete" @click="onTabelRowsDel()" :disabled="tableData.selections.length <= 0">删除</el-button>
			<el-table ref="tableRoleRef" :data="tableData.data" @selection-change="onTableSelectionChange" style="width: 100%" row-key="id">
				<el-table-column type="selection" align="cneter" :selectable="selectable" width="50" show-overflow-tooltip></el-table-column>
				<el-table-column type="index" align="cneter" width="50" show-overflow-tooltip></el-table-column>
				<el-table-column label="角色名称" prop="roleName" min-width="120" show-overflow-tooltip></el-table-column>
				<el-table-column label="权限字符" prop="roleKey" width="180" show-overflow-tooltip></el-table-column>
				<el-table-column label="状态" show-overflow-tooltip width="70">
					<template #default="scope">
						<span v-if="scope.row.status == '0'" class="color-primary">正常</span>
						<span v-else-if="scope.row.status == '1'" class="color-info">停用</span>
						<span v-else class="color-danger">删除</span>
					</template>
				</el-table-column>
				<el-table-column label="角色排序" prop="orderNum" width="80" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column label="备注" prop="remark" min-width="120" show-overflow-tooltip></el-table-column>
				<el-table-column label="创建时间" prop="createTime" width="180" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="125"
					v-auths="['system:role:edit', 'system:role:remove']">
					<template #default="scope">
						<el-button v-auth="'system:role:edit'" type="primary" link @click="onOpenEditRole(scope.row)"  :disabled="['SysAdmin','SaasAdmin'].includes(scope.row.roleKey)">修改</el-button>
						<el-button v-auth="'system:role:remove'" type="primary" link @click="onTabelRowDel(scope.row)" :disabled="['SysAdmin','SaasAdmin'].includes(scope.row.roleKey)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onPageSizeChange"
				@current-change="onPageCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30, 40, 50, 60]"
				v-model:current-page="search.pageNum"
				background
				v-model:page-size="search.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>
		<AddRole ref="addRoleRef" @submit="onTableSearch"/>
		<EditRole ref="editRoleRef" @submit="onTableSearch"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import AddRole from '/@/views/system/role/addRole.vue';
import EditRole from '/@/views/system/role/editRole.vue';
import { getRoleList, delRole } from '/@/api/system/role/index';
import { formatDate, getDateRangeShortcuts } from '/@/utils/formatTime';
export default {
	name: 'role',
	components: { AddRole, EditRole },
	setup() {
		const tableRoleRef = ref();
		const addRoleRef = ref();
		const editRoleRef = ref();
		const state = reactive({
			search:{
				roleName: undefined,
				roleKey: undefined,
				startTime: undefined,
				endTime: undefined,
				orderBy: 'orderNum',
				isAsc: 'asc',
				pageNum: 1,
				pageSize: 10
			},
			dateData: {
				value: [],
				shortcuts: getDateRangeShortcuts()
			},
			tableData: {
				selections: [],//选中的行数据
				data: [],
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params) => {
			getRoleList(Object.assign(state.search, params)).then((res) => {
				if(res.code == 0){
					state.tableData.data = res.data;
					state.tableData.total = res.total;
					tableRoleRef.value.clearSelection();
				}
			});
		}

		//查询
		const onTableSearch = () => {
			if(state.dateData.value.length == 2){
				state.search.startTime = formatDate(state.dateData.value[0], 'yyyy-MM-dd') + ' 00:00:00';
				state.search.endTime = formatDate(state.dateData.value[1], 'yyyy-MM-dd') + ' 23:59:59';
			}
			loadTableData(state.search);
		}
		//重置
		const onResetSearch = () => {
			state.dateData.value = [];
			state.search.roleName = undefined;
			state.search.roleKey = undefined;
			state.search.startTime = undefined;
			state.search.endTime = undefined;
			state.search.pageNum = 1;
			state.search.pageSize = 10;
			loadTableData(state.search);
		}
		// 监听表格选中行
		const onTableSelectionChange = (selections) =>{
			state.tableData.selections = selections;
		};
		// 每页显示数据大小改变
		const onPageSizeChange = (val: number) => {
			state.search.pageSize = val;
			loadTableData(state.search);
		};
		// 页码改变
		const onPageCurrentChange = (val: number) => {
			state.search.pageNum = val;
			loadTableData(state.search);
		};
		// 打开新增角色弹窗
		const onOpenAddRole = (row: object) => {
			addRoleRef.value.openDialog(row);
		};
		// 打开编辑角色弹窗
		const onOpenEditRole = (row: object) => {
			row = row || state.tableData.selections[0];
			editRoleRef.value.openDialog(row);
		};
		// 删除选中行
		const onTabelRowsDel = () => {
			const ids = state.tableData.selections.map(item => item.id);
			if(ids && ids.length > 0){
				ElMessageBox.confirm('确定删除选中的角色信息吗？', '提示', {
					confirmButtonText: '删除',
					cancelButtonText: '取消',
					type: 'warning',
				}).then(() => {
					delRole(ids).then((res) => {
						if(res.code === 0){
							ElMessage.success('删除成功');
							loadTableData();
						}
					});
				}).catch(() => {});
			}
		};
		// 删除当前行
		const onTabelRowDel = (row: object) => {
			ElMessageBox.confirm('确定删除该条【' + row.roleName + '】角色信息吗？', '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delRole([row.id]).then((res) => {
					if(res.code === 0){
						ElMessage.success('删除成功');
						loadTableData({});
					}
				});
			}).catch(() => {});
		};
		const selectable = (row: object, index: number) => {
			return !['SysAdmin','SaasAdmin'].includes(row.roleKey);
		}
		// 页面加载时
		onMounted(() => {
			loadTableData({});
		});
		return {
			tableRoleRef,
			addRoleRef,
			editRoleRef,
			onTableSearch,
			onTableSelectionChange,
			onResetSearch,
			onPageSizeChange,
			onPageCurrentChange,
			onOpenAddRole,
			onOpenEditRole,
			onTabelRowsDel,
			onTabelRowDel,
			selectable,
			...toRefs(state),
		};
	},
};
</script>